package com.banggood.common.config;

import com.banggood.pps.commons.interceptor.CrossDomainInterceptor;
import com.banggood.pps.commons.interceptor.RequestLogInterceptor;
import com.banggood.pps.commons.interceptor.login.OAuth2LoginVerifyInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.resource.GzipResourceResolver;

import java.util.concurrent.TimeUnit;

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

	@Autowired
	private CrossDomainInterceptor crossDomainInterceptor;

	@Autowired
	private RequestLogInterceptor requestLogInterceptor;

	@Autowired
	private OAuth2LoginVerifyInterceptor oAuth2LoginVerifyInterceptor;


	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {

		// TODO 修改前端路径、缓存配置等
		registry.addResourceHandler("/finder-front/static/**")
				.addResourceLocations("file:/pps/projects/bg-finder-front/finder-front/static/")
				.setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS)).resourceChain(true)
				.addResolver(new GzipResourceResolver());
		registry.addResourceHandler("/finder-front/**")
				.addResourceLocations("file:/pps/projects/bg-finder-front/finder-front/");
		super.addResourceHandlers(registry);
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(crossDomainInterceptor).addPathPatterns("/**");
		registry.addInterceptor(requestLogInterceptor).addPathPatterns("/**");
		registry.addInterceptor(oAuth2LoginVerifyInterceptor).addPathPatterns("/**").excludePathPatterns(
				"/swagger-resources/**", "/v2/api/**", "/configuration/**", "/logout", "/finder/attach/excel/preview",
				"/file/excel/preview", "/finder/attach/wxUploadFile", "/permission/systemAuthList");
		super.addInterceptors(registry);
	}

}
